package com.fireline.beauty.cashier;

import java.math.BigDecimal;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.layout.RowLayout;

import com.zgw.fireline.base.widgets.ControlUtil;

import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 * 联合结账窗口
 * */
public class LhjzDialog extends Dialog {

	protected int result;
	protected Shell shell;
	private Text textXfje;
	private Text textXj;
	private Text textCzk;
	private Text textXyk;
	private Text textDjq;
	private Label labZlje;
	private String knye;
	private String yfje;

	public String xj;
	public String xyk;
	public String czk;
	public String djq;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public LhjzDialog(Shell parent, String fkje, String knye) {
		super(parent, SWT.NONE);
		this.yfje = fkje;
		this.knye = knye;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public int open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(439, 320);
		shell.setText("联合结账");
		GridLayout gl_shell = new GridLayout(1, false);
		gl_shell.marginBottom = 10;
		shell.setLayout(gl_shell);

		Composite composite = new Composite(shell, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.verticalSpacing = 10;
		gl_composite.marginHeight = 20;
		gl_composite.marginLeft = 20;
		gl_composite.marginWidth = 10;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		lblNewLabel.setText("消费金额");

		textXfje = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		textXfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textXfje.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		textXfje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false,
				1, 1));
		textXfje.setText(yfje);
		ControlUtil.setCurrencyRule(textXfje, false, false);
		ControlUtil.setEnterToFocusMove(textXfje);
		new Label(composite, SWT.NONE);

		Label lblf = new Label(composite, SWT.NONE);
		lblf.setText("现金付款（F1）");
		lblf.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));

		textXj = new Text(composite, SWT.BORDER);
		textXj.setFocus();
		textXj.setText("0");
		textXj.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countZlje();
			}
		});
		textXj.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textXj.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		textXj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		ControlUtil.setCurrencyRule(textXj, false, false);
		ControlUtil.setEnterToFocusMove(textXj);
		Composite composite_3 = new Composite(composite, SWT.NONE);
		GridData gd_composite_3 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_composite_3.widthHint = 60;
		composite_3.setLayoutData(gd_composite_3);
		RowLayout rl_composite_3 = new RowLayout(SWT.HORIZONTAL);
		rl_composite_3.pack = false;
		composite_3.setLayout(rl_composite_3);

		Label label_1 = new Label(composite_3, SWT.NONE);
		label_1.setText("找零金额：");
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		labZlje = new Label(composite_3, SWT.NONE);
		labZlje.setText("0");
		labZlje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));

		Label lblf_1 = new Label(composite, SWT.NONE);
		lblf_1.setText("储值卡付款（F2）");
		lblf_1.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));

		textCzk = new Text(composite, SWT.BORDER);
		textCzk.setText("0");
		textCzk.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countZlje();
			}
		});
		textCzk.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textCzk.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		textCzk.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		ControlUtil.setCurrencyRule(textCzk, false, false);
		ControlUtil.setEnterToFocusMove(textCzk);
		{
			final Composite composite_2 = new Composite(composite, SWT.NONE);
			composite_2.setVisible(false);
			GridData gd_composite_2 = new GridData(SWT.LEFT, SWT.CENTER, false,
					false, 1, 1);
			gd_composite_2.widthHint = 60;
			composite_2.setLayoutData(gd_composite_2);
			RowLayout rl_composite_2 = new RowLayout(SWT.HORIZONTAL);
			rl_composite_2.pack = false;
			composite_2.setLayout(rl_composite_2);

			Label lblNewLabel_2 = new Label(composite_2, SWT.NONE);
			lblNewLabel_2.setForeground(SWTResourceManager
					.getColor(SWT.COLOR_BLUE));
			lblNewLabel_2.setText("卡内佘额：");

			Label lblNewLabel_3 = new Label(composite_2, SWT.NONE);
			lblNewLabel_3.setForeground(SWTResourceManager
					.getColor(SWT.COLOR_RED));
			lblNewLabel_3.setText(knye);
			textCzk.addFocusListener(new FocusListener() {

				@Override
				public void focusLost(FocusEvent e) {
					composite_2.setVisible(false);
				}

				@Override
				public void focusGained(FocusEvent e) {
					composite_2.setVisible(true);
				}
			});
		}

		Label lblf_2 = new Label(composite, SWT.NONE);
		lblf_2.setText("信用卡付款（F3）");
		lblf_2.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));

		textXyk = new Text(composite, SWT.BORDER);
		textXyk.setText("0");
		textXyk.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countZlje();
			}
		});
		textXyk.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textXyk.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		textXyk.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1));
		ControlUtil.setCurrencyRule(textXyk, false, false);
		ControlUtil.setEnterToFocusMove(textXyk);
		new Label(composite, SWT.NONE);

		Label lblf_3 = new Label(composite, SWT.NONE);
		lblf_3.setText("代金券付款（F4）");
		lblf_3.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));

		textDjq = new Text(composite, SWT.BORDER);
		textDjq.setText("0");
		textDjq.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				countZlje();
			}
		});
		textDjq.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textDjq.setFont(SWTResourceManager.getFont("宋体", 14, SWT.BOLD));
		textDjq.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1));
		ControlUtil.setCurrencyRule(textDjq, false, false);
		ControlUtil.setEnterToFocusMove(textDjq);
		new Label(composite, SWT.NONE);

		Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite_1, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doOkPressed();
			}
		});
		GridData gd_btnNewButton_1 = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton_1.heightHint = 25;
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("确定（F5）");

		Button btnNewButton = new Button(composite_1, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.heightHint = 25;
		gd_btnNewButton.widthHint = 80;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("取消（ESC）");
	}

	// 点击确定
	protected void doOkPressed() {
		BigDecimal je = new BigDecimal(0);
		je = je.add(new BigDecimal(textXyk.getText()));
		je = je.add(new BigDecimal(textCzk.getText()));
		je = je.add(new BigDecimal(textDjq.getText()));
		if (je.compareTo(new BigDecimal(yfje)) == 1) {
			MessageDialog.openInformation(shell, "提示", "付款总额大于应付金额");
			return;
		} else if (new BigDecimal(labZlje.getText()).floatValue() < 0) {
			MessageDialog.openInformation(shell, "提示", "付款总额小于应付金额");
			return;
		}
		xj = textXj.getText();
		xyk = textXyk.getText();
		czk = textCzk.getText();
		djq = textDjq.getText();

		result = SWT.OK;
		shell.close();
	}

	// 计算找零金额
	protected void countZlje() {
		BigDecimal je = new BigDecimal(0);
		BigDecimal bigYfje = new BigDecimal(yfje);
		je = je.add(new BigDecimal(textXyk.getText()));
		je = je.add(new BigDecimal(textCzk.getText()));
		je = je.add(new BigDecimal(textDjq.getText()));
		if (je.compareTo(bigYfje) == 1) { // 非现金 大于应付金额
			labZlje.setText(textXj.getText());
		} else {
			je = new BigDecimal(textXj.getText()).add(je).subtract(bigYfje);
			labZlje.setText(je.toString());
		}
	}

	public static void main(String[] args) {
		LhjzDialog dlg = new LhjzDialog(new Shell(), "160", "2788.99");
		dlg.open();
	}

}
